
package com.ifcar.app.activity.main;


import com.ifcar.app.activity.comment.CommentRankTabFragment;
import com.ifcar.app.activity.microhot.MicroHotBlogFragment;
import com.ifcar.app.activity.news.NewsMoreListFragment;
import com.ifcar.app.activity.picture.PictureWithViewFragment;
import com.ifcar.app.activity.tab.HomeFragment;
import com.ifcar.app.activity.tab.MoreFragment;
import com.ifcar.app.activity.topic.TopicFragment;
import com.ifcar.app.fragment.NRTabFragment;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.view.MyTabWidget;
import com.ifcar.app.view.MyTabWidget.AnimationViewCallback;
import com.ifcar.app.activity.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class HomeMainFragment extends NRTabFragment {
    public static final String TAG = "ifcar_main";
    
    private static final String HOME = "home";

    private static final String SEARCH = "search";

    private static final String MINE = "mine";

    private static final String INFO = "info";
    
    private static final String MORE = "more";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_main_layout, container, false);
    }

    @Override
    public int getContainerId() {
        return R.id.realtabcontent;
    }

    @Override
    public void onTabChanged(String tabId) {
        super.onTabChanged(tabId);
       
    }

    @Override
    protected void initTabhost(TabManager tabManager, TabHost tabHost) {
        final Context context = getActivity();
        MyTabWidget tabWidget = (MyTabWidget) tabHost.getTabWidget();
        ImageView animationView = new ImageView(context);
        animationView.setImageResource(R.drawable.tab_front_bg);
        tabWidget.setAnimaitonViewFront(true);
        tabWidget.setAnimaitonView(animationView, new AnimationViewCallback() {

            @Override
            public void onPrepareMeasure(View currentTabView, View animationView) {
            }

            @Override
            public Animation createAnimation(int current, int last) {
                TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                        last - current, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
                        0, Animation.RELATIVE_TO_SELF, 0);
                animation.setDuration(60);
                animation.setInterpolator(getActivity(), android.R.anim.decelerate_interpolator);
                return animation;
            }

        });
        

        addSpec(tabManager, tabHost, HOME, R.drawable.tab_home, HomeFragment.class,
                context, null);
        addSpec(tabManager, tabHost, INFO, R.drawable.tab_info, TopicFragment.class,
                context, null);
        addSpec(tabManager, tabHost, MINE, R.drawable.tab_mine,
                PictureWithViewFragment.class, context, null);  
        addSpec(tabManager, tabHost, SEARCH, R.drawable.tab_search,
                CommentRankTabFragment.class, context, null);
        addSpec(tabManager, tabHost, MORE, R.drawable.tab_more,
                MoreFragment.class, context, null);
        
    
    }

    private void addSpec(TabManager tabManager, TabHost tabHost, String tag, int titleImage,
            Class clazz, Context context, Bundle bundle) {
        TabSpec spec = tabHost.newTabSpec(tag);
        ImageView view = new ImageView(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.weight = 1;
        view.setLayoutParams(lp);
        if (titleImage != 0) {
            Drawable d = getResources().getDrawable(titleImage);
            //TODO ... 暂时先这样
            float totalWidth = getResources().getDisplayMetrics().widthPixels;
            int width = (int) (totalWidth / 5);
            int padding = Math.max(0, (width - d.getIntrinsicWidth()) / 2);
            lp.leftMargin = padding;
            lp.rightMargin = padding;
            
            
            view.setImageDrawable(d);
            view.setBackgroundResource(R.drawable.main_tab_bg_indicator);
        } else {
            view.setVisibility(View.INVISIBLE);
        }
        spec.setIndicator(view);

        tabManager.addTab(spec, clazz, bundle);
    }

    @Override
    public boolean onMenuOpen() {
        ViewGroup themeChangeMenu = (ViewGroup) getMenuView().findViewById(R.id.menu_theme_change);
        TextView title = (TextView) themeChangeMenu.getChildAt(1);
        ImageView image = (ImageView) themeChangeMenu.getChildAt(0);
        if (getThemeSettingsHelper().isNightTheme(getActivity())) {
            title.setText(R.string.theme_default);
            image.setImageResource(R.drawable.ic_menu_theme_default);
        } else {
            title.setText(R.string.theme_night);
            image.setImageResource(R.drawable.ic_menu_theme_night);
        }
        return super.onMenuOpen();
    }

    @Override
    public void onMenuClick(int id) {
       
    }

    @Override
    public void onApplyTheme() {
        super.onApplyTheme();
        
    }

   
}
